package com.weir.myboot.config;

import javax.servlet.FilterRegistration;
import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidConfig {
	/**
	 * 加载配置DataSource
	 * @return DruidDataSource
	 */
	@Bean
	@ConfigurationProperties("spring.datasource")
	public DataSource getDruidDataSource(){
		return new DruidDataSource();
	}
	
	/**
	 * 配置监控WEB监控资源
	 * @return servletRegistrationBean
	 */
	@Bean
	public ServletRegistrationBean getServletRegistrationBean(){
		 // 现在要进行druid监控的配置处理操作 
		ServletRegistrationBean servletRegistrationBean = 
				new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
		servletRegistrationBean.addInitParameter("allow", "127.0.0.1");		//白名单
		servletRegistrationBean.addInitParameter("deny", "129.1.1.1");			//黑名单
		servletRegistrationBean.addInitParameter("loginUsername", "admin");	//登陆用户名
		servletRegistrationBean.addInitParameter("loginPassword", "1234");		//登陆密码
		return servletRegistrationBean;
	}
	
	/**
	 * 配置过滤静态资源的监听器Bean
	 * @return filterRegistrationBean
	 */
	@Bean 
	public FilterRegistrationBean getFilterRegistrationBean(){
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		filterRegistrationBean.addUrlPatterns("/*");
		filterRegistrationBean.addInitParameter("exclusions",  "*.js,*.gif,*.jpg,*.css,/druid/*");
		return filterRegistrationBean;
	}
}
